package tcpproxymiddleware

import (
	"go-gateway-learn/dao"
	"go-gateway-learn/public"
)

func TcpFlowCountMiddleware() func(c *TcpSliceRouterContext){
	return func(c *TcpSliceRouterContext) {
		serverInterface := c.Get("service")
		if serverInterface == nil{
			c.conn.Write([]byte("get service emppty"))
			c.Abort()
			return
		}

		serviceDetail := serverInterface.(*dao.ServiceDetail)

		totalCount, err := public.FlowCounterHandler.GetCounter(public.FlowTotal)
		if err != nil{
			c.conn.Write([]byte(err.Error()))
			c.Abort()
			return
		}
		totalCount.Increase()

		serviceCounter, err := public.FlowCounterHandler.GetCounter(public.FlowServicePrefix + serviceDetail.Info.ServiceName)
		if err != nil{
			c.conn.Write([]byte(err.Error()))
			c.Abort()
			return
		}
		serviceCounter.Increase()
		c.next()
	}
}